/**
 * Created by ROGSTAR on 2016/11/7.
 */
require.config(window.jshead);
require(['jquery', 'pages', "layer",'navigator','zclip','query'], function ($, pg,layer) {
    layer.config({
        path: '/js/layer/'
    });
    var leftType=decodeURI(window.location.href).split('?')[3];
    var rightType=decodeURI(window.location.href).split('?')[4];
    function getInfo(){
        $.ajax({
            type: "POST",
            url: "/problem/typeList",
            data: {},
            dataType: "json",
            success: function (data) {
                if(data.result=="fail"){
                    layer.tips("数据请求失败");
                }else{
                    var typeName='';
                    for(var i=0;i<data.length;i++){
                        if(i==0){
                            typeName="<li style='border-top:0 none' data-type='"+data[i].id+"' class="+"active"+">"+data[i].typeName+"</li>";
                        }else{
                            typeName = typeName+"<li data-type='"+data[i].id+"'>"+data[i].typeName+'</li>';
                        }
                    }
                    $(".problem_ul").html(typeName);
                    if(leftType){
                        $(".problem_ul li").each(function(){
                            console.info( $(this).data().type);
                            if(leftType==$(this).data().type){
                                $(this).addClass("active").siblings().removeClass("active");
                            }
                        });
                        getList(leftType);
                    }else{
                        getList(1);
                    }
                    $(".problem_ul li").bind("click",function(){
                        $(this).addClass("active").siblings().removeClass("active");
                        getList($(this).data().type);
                    });
                }
            }
        });

    }
    getInfo();
    function getList(type){
        $.ajax({
            type: "POST",
            url: "/problem/problemList",
            dataType: "json",
            data:{pageSize:99,pageNum:1,type:type},
            success: function (data) {
                if(data.result=="fail"){
                    layer.tips("数据请求失败");
                }else{
                    var typeName='';
                    if(data.list.length>0){
                        for(var i=0;i<data.list.length;i++){
                           typeName+='<div class="problem_ul_question_all"><div class="problem_ul_question_item" id="question"><div class="title"><span id="'+data.list[i].id+'" class="zhankai fr zhankai_jia"></span><span class="wenzi_ask">'+data.list[i].answer+'</span></div><div class="problem_ul_ask_all"> <div class="problem_ul_ask_item"> <span>'+data.list[i].problem+'</span> </div> </div> </div> </div>';
                        }
                        $("#platformList").html(typeName);
                    }else{
                        $('#platformList').html("<img style='margin:30% 50%;transform: translate(-50%,-50%);' src='/img/zanwushuju.png'>");
                    }
                    $('.zhankai').bind('click',function(){
                        if($(this).hasClass("zhankai_jia")){
                            $(this).parent().siblings(".problem_ul_ask_all").show();
                            $(this).parent().parent().parent().siblings().children().children(".problem_ul_ask_all").hide();
                            $(this).removeClass("zhankai_jia").addClass("zhankai_jian");
                            $(this).parent().parent().parent().siblings().children().children().children(".zhankai").removeClass("zhankai_jian").addClass("zhankai_jia");
                        }else{
                            $(this).parent().siblings(".problem_ul_ask_all").hide();
                            $(this).removeClass("zhankai_jian").addClass("zhankai_jia");
                        }
                    });
                    if(rightType){
                        $(".zhankai").each(function(){
                            if(rightType==this.id){
                                $(this).click();
                            }
                        });
                    }
                }
            }
        });
    }
});